class MayBe {
  static of(value) {
    return new MayBe(value)
  }
  constructor(value) {
    this._value = value
  }
  map(fn) {
    // 处理方法是返回一个值为null的函子
    return this.isNothing() ? MayBe.of(null) : MayBe.of(fn(this._value))
  }
  isNothing() {
    return this._value === null || this._value === undefined
  }
}
const fp = require('lodash/fp')

let mayBe = MayBe.of([5, 6, 1])
let ex1 = () => {
  // 和map的区别就是函数放在第一位，迭代对象放在第二位
  return fp.map((x) => fp.add(x, 1), mayBe._value)
}
console.log(ex1())
console.log(mayBe._value)

let ex2 = () => {
  return fp.first(mayBe._value)
}
console.log(ex2())

// const { Maybe, Container } = require('./support')
// const fp = require('lodash/fp')
let safeProp = fp.curry(function (x, o) {
  return MayBe.of(o[x])
})
let user = { id: 2, name: 'Albert' }
let ex3 = () => {
  // 找到user首字母
  return fp.first(safeProp('name', user)._value)
}
console.log(ex3())
// console.log(user.name[0])

// let ex4 = function (n) {
//   if (n) {
//     return parseInt(n)
//   }
// }
let ex4 = (x) => parseInt(MayBe.of(x)._value)
// .map((x) => parseInt(x))
console.log(ex4(null))
